﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using app.Models;
using AmuTools;
using System.Net.Mail;
using System.Text;
using System.Text.RegularExpressions;

namespace app.Api
{
    /*
      
    */
    public class ApiPhone : ApiBase
    {
        public ApiPhone(Controller ctx) : base(ctx)
        {
        }

        public void Do(string action)
        {
            Phone ph = G.DB.Get<Phone>(new Cd().And("selected = 1").Render()).GetFirstEntity();
            switch (action)
            {
                case "add":
                    {
                        string accessKeyId = GetStringParam(new ReqStringParamOption { Name = "name" });
                        string accessKeysecret = GetStringParam(new ReqStringParamOption { Name = "password" });
                        string signName = GetStringParam(new ReqStringParamOption { Name = "signname" });
                        Phone p = new Phone { accessKeyId = accessKeyId, accessKeySecret = accessKeysecret,sign = signName, selected = 0 };
                        int r = G.DB.Insert<Phone>(p);
                        ResponseJson(new BooleanResult { value = r > 0 });
                        break;
                    }
                case "get_list":
                    {
                        ResponseJson(new ListResult<Phone> { list = G.DB.Get<Phone>().GetFirstTableList() });
                        break;
                    }
                case "select":
                    {
                        Cd cd = new Cd();
                        cd.And("selected = 1");
                        Phone p = G.DB.Get<Phone>(cd.Render()).GetFirstEntity();
                        if(p != null)
                        {
                            p.selected = 0;
                            G.DB.Update<Phone>(p);
                        }
                        p = G.DB.GetById<Phone>(req_id);
                        p.selected = 1;
                        ResponseJson(new BooleanResult { value = G.DB.Update<Phone>(p) > 0 });
                        MesHelper.UpdateConfig();
                        break;
                    }
                case "modify":
                    {
                        string accessKeyId = GetStringParam(new ReqStringParamOption { Name = "name" });
                        string accessKeysecret = GetStringParam(new ReqStringParamOption { Name = "password" });
                        string signName = GetStringParam(new ReqStringParamOption { Name = "signName" });
                        Phone p = new Phone { accessKeyId = accessKeyId, accessKeySecret = accessKeysecret, sign = signName, id = req_id };
                        int r = G.DB.Update<Phone>(p);
                        ResponseJson(new BooleanResult { value = r > 0 });
                        break;
                    }
                case "delete":
                    {
                        ResponseJson(new BooleanResult { value = G.DB.Delete<Phone>(req_id) > 0 });
                        break;
                    }
                case "check_phonenumber":
                    {
                        string num = GetStringParam(new ReqStringParamOption { Name = "phonenumber" });
                        
                        if (check_phonenumber(num))
                        {
                            ResponseJson(new BooleanResult { value = true });
                        }
                        else
                        {
                            ResponseJson(new BooleanResult { value = false });
                        }
                        break;
                    }
                case "send_auth_code":
                    {
                        //int status = GetIntParam(new ReqIntParamOption { Name = "status" });
                        int code_2 = GetIntParam(new ReqIntParamOption { Name = "type_2" });
                        string nowTime = DateTimeEx.DateTimeToTimeStamp(DateTime.Now.AddMinutes(-1));
                        if (MesHelper.CheckPhoneNumber(req_mobile_phone))
                        {
                            ctx.Session.Remove("checkcodestatus");
                            string checkCode = MesHelper.GetCode();
                            //未设置session
                            ctx.Session["auth_code"] = checkCode;
                            ctx.Session["auth_mobile_phone"] = req_mobile_phone;
                            ctx.Session["get_auth_code_time"] = DateTimeEx.GetNowTimeStamp();
                            //string result = "success";
                            string result = "";
                            Member m = G.DB.GetById<Member>(UserCode);
                            if(ph != null)
                            switch (code_2)
                            {
                                case 1:
                                    {
                                        result = MesHelper.SendRegisterMes(req_mobile_phone, checkCode);
                                        break;
                                    }
                                case 2:
                                    {
                                        result = MesHelper.SendRepassMes(req_mobile_phone, checkCode, m.username);
                                        break;
                                    }
                                case 3:
                                    {
                                        result = MesHelper.SendBindMes(req_mobile_phone, checkCode, m.username);
                                        break;
                                    }
                                case 4:
                                    {
                                        result = MesHelper.SendUnbindMes(req_mobile_phone, checkCode, m.username);
                                        break;
                                    }
                                default:
                                    {
                                        ResponseJson(new BooleanResult { value = false, message = "模板请求错误" });
                                        break;
                                    }
                            }
                            else
                            {
                                ResponseJson(new BooleanResult { value = false, message = "服务器出错，请联系管理员。" });
                            }
                            if (result == "success")
                            {
                                ResponseJson(new BooleanResult { value = true});
                            }
                            else
                            {
                                ResponseJson(new BooleanResult { value = false, message = result });
                            }
                        }
                        else
                        {
                            ResponseJson(new BooleanResult { value = false, message = "手机号格式不正确" });
                        }
                        break;
                    }
                case "check_auth_code":
                    {
                        string auth_code = GetStringParam(new ReqStringParamOption { Name = "auth_code" });
                        string nowTime = DateTimeEx.DateTimeToTimeStamp(DateTime.Now.AddMinutes(-5));
                        string beforeTime = ctx.Session["get_auth_code_time"].ToString();
                        string old_auth_code = ctx.Session["auth_code"].ToString();
                        if (string.Compare(nowTime, beforeTime) < 0)
                        {
                            if (old_auth_code.Equals(auth_code))
                            {
                                
                                ctx.Session["checkcodestatus"] = "success";
                                ResponseJson(new BooleanResult { value = true });
                            }
                            else
                            {
                                ResponseJson(new BooleanResult { value = false, message = "验证码错误" });
                            }
                        }
                        else
                        {
                            ResponseJson(new BooleanResult { value = false, message = "超时输入" });
                        }

                        break;
                    }
            }
        }

        public bool check_phonenumber(string num)
        {
            Cd cd = new Cd();
            cd.And("mobile_phone='{0}'", num);
            int res = G.DB.GetCount<Member>(cd.Render());
            if (res == 0)
            {
                return true;
            }
            return false;
        }
    }
}